package com.oocl.admin_platform.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.oocl.admin_platform.dao.FailReasonDao;
import com.oocl.admin_platform.db.factory.DBConnectionFactory;
import com.oocl.admin_platform.pojo.FailReason;
import com.oocl.admin_platform.util.DBUtil;
import com.oocl.admin_platform.util.UUIDEasyUtil;

public class FailReasonDaoImpl implements FailReasonDao{
	
	private static Logger logger = Logger.getLogger(FailReasonDaoImpl.class);
	
	@Override
	public FailReason findByMerchantId(String merchantId) {
		// TODO Auto-generated method stub
		String sql = "select * from failReason where merchantid=?";
		logger.info("enter findByMerchantId method id:   "+merchantId);
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		FailReason failReason=null;
		connection=DBConnectionFactory.getInstance();
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, merchantId);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				failReason=new FailReason();
				failReason.setId(resultSet.getString("id"));
				failReason.setMerchantId(resultSet.getString("MERCHANTID"));
				failReason.setReasonContent(resultSet.getString("reasoncontent"));
			}
		} catch (SQLException e) {
			logger.error("fail on the fail reason find by merchant id "+merchantId);
		} finally {
			DBUtil.close(connection, preparedStatement, resultSet);
		}
		return failReason;
	}

	@Override
	public FailReason findByMerchantUname(String uname) {
		// TODO Auto-generated method stub
		
		String sql = "select f.id,MERCHANTID, reasoncontent from failReason f inner join merchant m on f.MERCHANTID=m.id where m.uname=?";
		logger.info("enter findByMerchantUname method uname:   "+uname);
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		FailReason failReason=null;
		connection=DBConnectionFactory.getInstance();
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, uname);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				failReason=new FailReason();
				failReason.setId(resultSet.getString(1));
				failReason.setMerchantId(resultSet.getString("MERCHANTID"));
				failReason.setReasonContent(resultSet.getString("reasoncontent"));
			}
		} catch (SQLException e) {
			logger.error("fail on the fail reason find by merchant uname "+uname);
		} finally {
			DBUtil.close(connection, preparedStatement, resultSet);
		}
		return failReason;
	}

	@Override
	public boolean save(FailReason failReason) {
		// TODO Auto-generated method stub
		String sql = "insert into FAILREASON(id,merchantid,reasoncontent) values (?,?,?)";
		logger.info("enter save method failReason content:   "+failReason.getReasonContent());
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		//connection = DBUtil.getConnection();
		connection=DBConnectionFactory.getInstance();
		String[] idStrings = UUIDEasyUtil.getUUIDsInCount(1);
		int m = 0;
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, idStrings[0]);
			preparedStatement.setString(2, failReason.getMerchantId());
			preparedStatement.setString(3, failReason.getReasonContent());
			m = preparedStatement.executeUpdate();
			if (m>0) {
				logger.info("save success:   "+failReason.getReasonContent());
				return true;
			}
			else{
				logger.error("fail on the fail reason find by merchant uname : "+failReason.getReasonContent());
			}
		} catch (SQLException e) {
			logger.error("fail on the fail reason find by merchant uname exception: "+e.getMessage());
			
		} finally {
			DBUtil.close(connection, preparedStatement, null);
		}
		return false;
	}

	@Override
	public List<FailReason> findAll() {
		// TODO Auto-generated method stub
		List<FailReason> failReasonList = new ArrayList<FailReason>();
		String sql = "select * from failreason";
		logger.info("enter findAll method");
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		//connection = DBUtil.getConnection();
		connection=DBConnectionFactory.getInstance();
		try {
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			while (resultSet.next()) {
				FailReason failReason=new FailReason();
				failReason.setId(resultSet.getString("id"));
				failReason.setMerchantId(resultSet.getString("merchantid"));
				failReason.setReasonContent(resultSet.getString("reasoncontent"));
				failReasonList.add(failReason);
			}
		} catch (SQLException e) {
			logger.info("fail on the fail reason find all exception: "+e.getMessage());
		} finally {
			DBUtil.close(connection, preparedStatement, resultSet);
		}
		return failReasonList;
	}

	@Override
	public boolean update(FailReason failReason) {
		// TODO Auto-generated method stub
		String sql="update failreason set reasoncontent=? where merchantid=?";
		logger.info("enter update method failReason content:   "+failReason.getReasonContent());
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		//connection = DBUtil.getConnection();
		connection=DBConnectionFactory.getInstance();
		int m = 0;
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, failReason.getReasonContent());
			preparedStatement.setString(2, failReason.getMerchantId());
			m = preparedStatement.executeUpdate();
			
			if (m>0) {
				logger.info("success update method failReason content:   "+failReason.getReasonContent());
				return true;
			}else{
				
			}
		} catch (SQLException e) {
			logger.error("fail on update method failReason content:   "+failReason.getReasonContent());
		} finally {
			DBUtil.close(connection, preparedStatement, null);
		}
		return false;
	}

	@Override
	public boolean delete(String merchantId) {
		// TODO Auto-generated method stub
		String sql="delete from failReason where merchantid=?";
		logger.info("enter delete method merchantId:   "+merchantId);
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		//connection = DBUtil.getConnection();
		connection=DBConnectionFactory.getInstance();
		int m = 0;
		try {
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, merchantId);
			m = preparedStatement.executeUpdate();
			
			if (m>0) {
				logger.info("success delete method merchantId:   "+merchantId);
				return true;
			}
		} catch (SQLException e) {
			logger.error("fail on delete method merchantId:   "+merchantId);
		} finally {
			DBUtil.close(connection, preparedStatement, null);
		}
		return false;
	}

}
